package cn.darkhorse.mesadmin.controller.alarm;


import cn.darkhorse.alarm.dto.AlarmRecordInfoDTO;
import cn.darkhorse.alarm.pojo.AlarmRecordInfo;
import cn.darkhorse.alarm.service.AlarmRecordInfoService;
import cn.darkhorse.alarm.vo.AlarmRecordInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报警记录表 前端控制器
 * </p>
 *
 * @author maoguanjun
 * @since 2023-04-07
 */
@RestController
@RequestMapping("/alarm/alarmRecordInfo")
public class AlarmRecordInfoController extends BaseController {
    @Resource
    private AlarmRecordInfoService alarmRecordInfoService;

    @Resource
    private ISysUserService userService;
    /**
     * 列表
     * @param alarmRecordInfoDTO
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public TableDataInfo list(AlarmRecordInfoDTO alarmRecordInfoDTO){

        List<AlarmRecordInfoVO> recordInfoList=alarmRecordInfoService.listAll(alarmRecordInfoDTO);
        return getDataTable(recordInfoList);
    }


    /**
     * 列表 === 设备参数内的告警
     * @param alarmRecordInfoDTO
     * @return
     */
    @RequestMapping(value = "/device/list",method = RequestMethod.GET)
    public AjaxResult deviceAlarmlist(AlarmRecordInfoDTO alarmRecordInfoDTO){
        Map<String,Object> result = new HashMap<>();
        startPage();
        List<AlarmRecordInfoVO> recordInfoList=alarmRecordInfoService.listAll(alarmRecordInfoDTO);
        LambdaQueryWrapper<AlarmRecordInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AlarmRecordInfo::getDeviceId,alarmRecordInfoDTO.getDeviceId())
                    .eq(AlarmRecordInfo::getAlarmStatus,0);
        long untreated = alarmRecordInfoService.count(queryWrapper);
        result.put("list",getDataTable(recordInfoList));
        result.put("untreated",untreated);
        return AjaxResult.success(result);
    }




    @RequestMapping(value = "/device/export",method = RequestMethod.POST)
    public void exportDeviceAlarm(HttpServletResponse response,AlarmRecordInfoDTO alarmRecordInfoDTO){
        startPage();
        List<AlarmRecordInfoVO> recordInfoList=alarmRecordInfoService.listAll(alarmRecordInfoDTO);
        for (AlarmRecordInfoVO recordInfoVO : recordInfoList) {
            SysUser sysUser = userService.selectUserById(recordInfoVO.getHandler());
            if(null==sysUser){
                recordInfoVO.setHandlerName("");
                continue;
            }
            recordInfoVO.setHandlerName(sysUser.getNickName());
        }
        ExcelUtil<AlarmRecordInfoVO> util = new ExcelUtil<>(AlarmRecordInfoVO.class);
        util.exportExcel(response,recordInfoList,"设备告警列表");
    }

    /**
     * 查看
     * @param recordId
     * @return
     */
    @RequestMapping(value = "/view",method = RequestMethod.POST)
    public AjaxResult view(Long recordId){
        if(null==recordId){
            return  AjaxResult.error("缺失参数！");
        }
        AlarmRecordInfoVO recordInfo= alarmRecordInfoService.view(recordId);
        return  AjaxResult.success(recordInfo);
    }

    @RequestMapping(value = "/handle-single",method = RequestMethod.POST)
    public AjaxResult handleSingle(Long recordId,String handleDescription){
        if(null==recordId){
            return AjaxResult.error("缺失记录参数！");
        }
        boolean b=alarmRecordInfoService.handleSingle(recordId,handleDescription);
        return AjaxResult.success(b);
    }
    @RequestMapping(value = "/handle-single/pic",method = RequestMethod.POST)
    public AjaxResult handleSinglePic(Long recordId,String handleDescription,String alarmPic){
        if(null==recordId){
            return AjaxResult.error("缺失记录参数！");
        }
        boolean b=alarmRecordInfoService.handleSingle(recordId,handleDescription,alarmPic);
        return AjaxResult.success(b);
    }

    /**
     * 根据设备ID 批量清除未处理告警
     * @param deviceId
     * @param handleDescription
     * @return
     */
    @RequestMapping(value = "/handle-device",method = RequestMethod.POST)
    public AjaxResult handleByDeviceId(Long deviceId,String handleDescription){
        if(null==deviceId){
            return AjaxResult.error("缺失记录参数！");
        }
        boolean b=alarmRecordInfoService.handleByDeviceId(deviceId,handleDescription);
        return AjaxResult.success(b);
    }

    /**
     * 根据参数ID  批量清楚未处理告警
     * @param paramterId
     * @param handleDescription
     * @return
     */
    @RequestMapping(value = "/handle-parameter",method = RequestMethod.POST)
    public AjaxResult handleByParameterId(Long paramterId,String handleDescription){
        if(null==paramterId){
            return AjaxResult.error("缺失记录参数！");
        }
        boolean b=alarmRecordInfoService.handleByParameterId(paramterId,handleDescription);
        return AjaxResult.success(b);
    }


    @RequestMapping(value = "/handle-device/pic",method = RequestMethod.POST)
    public AjaxResult handleByDeviceIdPic(Long deviceId,String handleDescription,String alarmPic){
        if(null==deviceId){
            return AjaxResult.error("缺失记录参数！");
        }
        boolean b=alarmRecordInfoService.handleByDeviceId(deviceId,handleDescription,alarmPic);
        return AjaxResult.success(b);
    }
    @RequestMapping(value = "/handle-parameter/pic",method = RequestMethod.POST)
    public AjaxResult handleByParameterIdPic(Long paramterId,String handleDescription,String alarmPic){
        if(null==paramterId){
            return AjaxResult.error("缺失记录参数！");
        }
        boolean b=alarmRecordInfoService.handleByParameterId(paramterId,handleDescription,alarmPic);
        return AjaxResult.success(b);
    }

    @RequestMapping(value = "/lsit-last",method = RequestMethod.POST)
    public AjaxResult ListLast(){
        List<AlarmRecordInfoVO> listLast=alarmRecordInfoService.listLastFive();
        return AjaxResult.success(listLast);
    }

    /**
     * 是否存在待审记录true：存在false:不存在
     * @param deviceId
     * @param paramterId
     * @return
     */
    @RequestMapping(value = "/has-alarm",method = RequestMethod.POST)
    public AjaxResult hasAlarm(Long deviceId,Long paramterId){
        if(null==deviceId&&null==paramterId){
            return AjaxResult.error("缺失参数！");
        }
        if(null!=deviceId&&null!=paramterId){
            return AjaxResult.error("设备ID与参数ID只能传一个！");
        }
        boolean result=alarmRecordInfoService.hasAlarm(deviceId,paramterId);
        return AjaxResult.success(result);
    }


    /**
     * 全部清楚未处理告警信息
     * @param handleDescription
     * @return
     */
    @RequestMapping(value = "/clear-all",method = RequestMethod.POST)
    public AjaxResult clearAll(String handleDescription){
        boolean b=alarmRecordInfoService.clearAll(handleDescription);
        return AjaxResult.success(b);
    }



}

